package com.poe.controller.email;

import com.poe.vo.UserReturn;
import com.util.Email;
import com.util.Util;
import net.sf.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@Scope("singleton")
public class EmailController {
	String emailCode="";
	@RequestMapping("getEmail")
	@ResponseBody
	public void getEmail(HttpServletResponse response, String emailAddress, String emailCode1) throws IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw=response.getWriter();
		
		Email email=new Email();
		UserReturn userReturn=new UserReturn();
		if(emailCode1==null||emailCode1.equals("")){
			emailCode= Util.getUUId().substring(0, 6);
			boolean bl=email.getEmail(emailCode, emailAddress);
				if(bl){
					userReturn.setNum(1);
					String a=JSONObject.fromObject(userReturn).toString();
			    	pw.write(a);
			    	pw.flush();
			    	pw.close();
				}else{
					userReturn.setNum(0);
					String a= JSONObject.fromObject(userReturn).toString();
			    	pw.write(a);
			    	pw.flush();
			    	pw.close();
				}
		}else{
			System.out.println(emailCode+"**"+emailCode1);
			if(emailCode.equals(emailCode1)){
				userReturn.setNum(1);
				String a=JSONObject.fromObject(userReturn).toString();
		    	pw.write(a);
		    	pw.flush();
		    	pw.close();
			}else{
				userReturn.setNum(0);
				String a=JSONObject.fromObject(userReturn).toString();
		    	pw.write(a);
		    	pw.flush();
		    	pw.close();
			}
		}
		
	}
}
